package com.ykt.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

/**
 * 对基础类型拆箱(Unboxing)，避免空指针异常，类名缩写
 *
 * @author: long.hua
 * @date: 2015-12-10 20:45
 * @since 1.0
 */
public class UB {

    private final static Logger LOGGER = LoggerFactory.getLogger(UB.class);

    /**
     * 返回整型，默认为-1
     *
     * @param v
     * @return
     */
    public static int i(Object v) {
        return i(v, -1);
    }

    public static int i(Object v, int defaultValue) {
        if (v == null) {
            return defaultValue;
        }

        if (v instanceof Byte || v instanceof Short || v instanceof Integer) {
            return Integer.valueOf(v.toString());
        }

        try {
            if (v instanceof String) {
                return Integer.valueOf(v.toString());
            }

            if (v instanceof BigDecimal) {
                return ((BigDecimal) v).intValueExact();
            }
        } catch (NumberFormatException e) {
            LOGGER.error("Unboxing error, param:{}", v, e);
        }

        return defaultValue;
    }

    /**
     * 返回长整型，默认为-1
     *
     * @param v
     * @return
     */
    public static long j(Object v) {
        return j(v, -1);
    }

    public static long j(Object v, int defaultValue) {
        if (v == null) {
            return defaultValue;
        }

        if (v instanceof Byte || v instanceof Short || v instanceof Integer || v instanceof Long) {
            return Long.valueOf(v.toString());
        }

        try {
            if (v instanceof String) {
                return Long.valueOf(v.toString());
            }

            if (v instanceof BigDecimal) {
                return ((BigDecimal) v).longValueExact();
            }
        } catch (Exception e) {
            LOGGER.error("Unboxing error, param:{}", v, e);
        }

        return defaultValue;
    }

    /**
     * 返回短整型，默认为-1
     *
     * @param v
     * @return
     */
    public static short s(Object v) {
        return s(v, (short) -1);
    }

    public static short s(Object v, short defaultValue) {
        if (v == null) {
            return defaultValue;
        }

        if (v instanceof Byte || v instanceof Short) {
            return Short.valueOf(v.toString());
        }

        try {
            if (v instanceof String) {
                return Short.valueOf(v.toString());
            }

            if (v instanceof BigDecimal) {
                return ((BigDecimal) v).shortValueExact();
            }
        } catch (Exception e) {
            LOGGER.error("Unboxing error, param:{}", v, e);
        }

        return defaultValue;
    }

    /**
     * 返回字节型，默认为-1
     *
     * @param v
     * @return
     */
    public static byte b(Object v) {
        return b(v, (byte) -1);
    }

    public static byte b(Object v, byte defaultValue) {
        if (v == null) {
            return defaultValue;
        }

        if (v instanceof Byte) {
            return Byte.valueOf(v.toString());
        }

        try {
            if (v instanceof String) {
                return Byte.valueOf(v.toString());
            }

            if (v instanceof BigDecimal) {
                return ((BigDecimal) v).byteValueExact();
            }
        } catch (Exception e) {
            LOGGER.error("Unboxing error, param:{}", v, e);
        }

        return defaultValue;
    }

    /**
     * 返回字符，默认为 0 号字符
     *
     * @param v
     * @return
     */
    public static char c(Character v) {
        return c(v, (char) 0);
    }

    public static char c(Character v, char defaultValue) {
        if (v == null) {
            return defaultValue;
        } else {
            return v;
        }
    }

    /**
     * 返回单精度浮点型，默认为-1
     *
     * @param v
     * @return
     */
    public static float f(Object v) {
        return f(v, -1);
    }

    public static float f(Object v, float defaultValue) {
        if (v == null) {
            return defaultValue;
        }

        if (v instanceof Float) {
            return (float) v;
        }

        if (v instanceof BigDecimal) {
            return ((BigDecimal) v).floatValue();
        }

        if (v instanceof String) {
            try {
                return Float.valueOf(v.toString());
            } catch (NumberFormatException e) {
                LOGGER.error("Unboxing error, param:{}", v, e);
            }
        }

        return defaultValue;
    }

    /**
     * 返回双精度浮点型，默认为-1
     *
     * @param v
     * @return
     */
    public static double d(Object v) {
        return d(v, -1);
    }

    public static double d(Object v, double defaultValue) {
        if (v == null) {
            return defaultValue;
        }

        if (v instanceof Float || v instanceof Double) {
            return Double.valueOf(v.toString());
        }

        if (v instanceof BigDecimal) {
            return ((BigDecimal) v).doubleValue();
        }

        if (isIntegralType(v)) {
            return Double.valueOf(v.toString());
        }

        if (v instanceof String) {
            try {
                return Double.valueOf(v.toString());
            } catch (NumberFormatException e) {
                LOGGER.error("Unboxing error, param:{}", v, e);
            }
        }

        return defaultValue;
    }

    /**
     * 返回字节型，默认为 false
     *
     * @param v
     * @return
     */
    public static boolean z(Object v) {
        return z(v, false);
    }

    public static boolean z(Object v, boolean defaultValue) {
        if (v == null) {
            return defaultValue;
        }

        if (v instanceof Boolean) {
            return (boolean) v;
        }

        if (isIntegralType(v)) {
            return j(v) > 0; // 大于返回true，小于等于0返回false
        }

        if (v instanceof String) {
            return Boolean.valueOf(v.toString());
        }

        return defaultValue;
    }

    private static boolean isIntegralType(Object v) {
        if (v instanceof Byte || v instanceof Short || v instanceof Integer || v instanceof Long) {
            return true;
        }
        return false;
    }

}

